package com.ukao.cashregister.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mcxtzhang.commonadapter.rv.CommonAdapter;
import com.mcxtzhang.commonadapter.rv.ViewHolder;
import com.ukao.cashregister.R;
import com.ukao.cashregister.bean.BatchlTabBean;
import com.ukao.cashregister.listener.OnItemChildClickListener;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.widget.StateButton;

import java.util.List;

import cn.bingoogolapple.qrcode.core.DisplayUtils;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

/**
 * Created by ukaodev on 2017/6/14.
 */

public class ActoryBatchAdapter extends CommonAdapter<BatchlTabBean.ListBean> {




    public ActoryBatchAdapter(Context context, List<BatchlTabBean.ListBean> datas) {
        super(context, datas, R.layout.adapter_actory_batch);
    }
    private  OnItemChildClickListener  mOnItemChildClickListener;
    public  void  setOnItemChildClickListener(OnItemChildClickListener mOnItemChildClickListener){
        this.mOnItemChildClickListener=mOnItemChildClickListener;
    }



    @Override
    public void convert(final  ViewHolder holder,final BatchlTabBean.ListBean item) {

        TextView    code=holder.getView(R.id.code);
        TextView    storeName=holder.getView(R.id.storeName);
        TextView    factoryName=holder.getView(R.id.factoryName);
        TextView    statusText=holder.getView(R.id.statusText);
        TextView    productCnt=holder.getView(R.id.productCnt);
        TextView    logisticsWorkName=holder.getView(R.id.logisticsWorkName);
        StateButton    isFinish=holder.getView(R.id.isFinish);
        StateButton    deleteBtn=holder.getView(R.id.delete_btn);
        final   ImageView qrCode=holder.getView(R.id.qrCode);
        code.setText(item.getCode());
        storeName.setText(CheckUtils.isEmptyString(item.getStoreName()));
        factoryName.setText(CheckUtils.isEmptyString(item.getFactoryName()));
        statusText.setText(CheckUtils.isEmptyString(item.getStatusText()));
        productCnt.setText(item.getProductCnt()+item.getAnnexCnt()+"");
        StateButton    printBtn=holder.getView(R.id.print_btn);


        //生成二维码，第一个参数为要生成的文本，第二个参数为生成尺寸，第三个参数为生成回调
        QRCodeEncoder.encodeQRCode(item.getCode(), DisplayUtils.dp2px(mContext,mContext.getResources().getDimension(R.dimen.widget_size_160)), new QRCodeEncoder.Delegate() {
            /**
             * 生成成功
             * @param bitmap
             */
            @Override
            public void onEncodeQRCodeSuccess(Bitmap bitmap) {
                item.setmBitmap(bitmap);
                qrCode.setImageBitmap(bitmap);
            }

            /**
             * 生成失败
             */
            @Override
            public void onEncodeQRCodeFailure() {
                qrCode.setImageResource(R.drawable.load_image);
            }
        });



        View.OnClickListener  OnClickListener= new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!CheckUtils.isNull(mOnItemChildClickListener))
                    mOnItemChildClickListener.onChildItemClick(v.getId(), getPosition(holder));


            }
        };
        logisticsWorkName.setText("物流人员："+CheckUtils.isEmptyString(item.getLogisticsWorkName())+CheckUtils.isEmptyString(item.getLogisticsWorkPhone()));
        if (item.getIsFinish()){
            printBtn.setVisibility(View.VISIBLE);
            isFinish.setVisibility(View.GONE);
            deleteBtn.setVisibility(View.GONE);

        }else{
            printBtn.setVisibility(View.GONE);
            isFinish.setVisibility(item.getProductCnt() == 0 ? View.GONE : View.VISIBLE);
            deleteBtn.setVisibility(View.VISIBLE);
        }

        printBtn.setOnClickListener(OnClickListener);
        deleteBtn.setOnClickListener(OnClickListener);
        isFinish.setOnClickListener(OnClickListener);
    }


}
